{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 01 模块"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.1 模块的概念"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 模块是 Python 程序架构的一个核心概念\n",
    "# • 每一个以扩展名 py 结尾的 Python 源代码文件都是一个 模块\n",
    "# • 模块名 同样也是一个 标识符，需要符合标识符的命名规则\n",
    "# • 在模块中定义的 全局变量 、函数、类 都是提供给外界直接使用的 工具\n",
    "# • 模块 就好比是 工具包，要想使用这个工具包中的工具，就需要先 导入 这个模块"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 1.2 导入方式"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模块1\n",
      "我是模块1 \n"
     ]
    }
   ],
   "source": [
    "# import\n",
    "\n",
    "import wd_01 as DogModule\n",
    "# 模块别名 应该符合 大驼峰命名法\n",
    "print(DogModule.title)\n",
    "DogModule.say_hello()\n",
    "dog = DogModule.Dog()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是模块1 \n"
     ]
    }
   ],
   "source": [
    "# from...import...\n",
    "\n",
    "# • 从某一个模块导入 部分 工具，就可以使用 from ... import 的方式\n",
    "# • import 模块名 是 一次性 把模块中 所有工具全部导入，并且通过 模块名/别名访问\n",
    "\n",
    "from wd_01 import say_hello\n",
    "\n",
    "say_hello()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# tips:\n",
    "# 如果 两个模块，存在 同名的函数，那么 后导入模块的函数，会 覆盖掉先导入的函数\n",
    "# • 开发时 import 代码应该统一写在 代码的顶部，更容易及时发现冲突\n",
    "# • 一旦发现冲突，可以使用 as 关键字 给其中一个工具起一个别名"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Program Files\\Python39\\lib\\random.py\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "# Python 中每一个模块都有一个内置属性 __file__ 可以 查看模块 的 完整路径\n",
    "import random\n",
    "print(random.__file__)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 02 包"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 概念\n",
    "# • 包 是一个包含多个模块的特殊目录\n",
    "# • 目录下有一个特殊的文件 __init__.py\n",
    "# • 包名的命名方式和变量名一致，小写字母 + _\n",
    "# 好处\n",
    "# • 使用import包名，可以一次性导入包中所有的模块"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 1 新建一个 wd_message 的包\n",
    "# 2 在目录下，新建两个文件 send_message .py 和 receive_message.py\n",
    "# 3 在 send_message 文件中定义一个 send 函数\n",
    "# 4 在 receive_message 文件中定义一个 receive 函数\n",
    "# 5 在外部直接导入 wd_message 的包"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "发送消息\n",
      "接收文件\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "import wd_message\n",
    "\n",
    "wd_message.send_message.send()\n",
    "txt = wd_message.receive_message.receive()\n",
    "print(txt)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 03 发布模块"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# • 如果希望自己开发的模块，分享 给其他人，可以按照以下步骤操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.1 制作发布压缩包步骤"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 新建发布模块的文件夹，然后将 setup.py 放入(setup.py 和要发布的模块在同一级目录)\n",
    "\n",
    "# 1)创建setup.py\n",
    "# • setup.py 的文件\n",
    "from distutils.core import setup\n",
    "setup(name=\"wd_message\", # 包名\n",
    "      version=\"1.0\", # 版本\n",
    "      description=\"发送和接收消息模块\", # 描述信息\n",
    "      long_description=\"完整的发送和接收消息模块\", # 完整描述信息\n",
    "      author=\"Dennis\", # 作者\n",
    "      author_email=\" 1759159441@qq.com\", # 作者邮箱\n",
    "      py_modules=[\"wd_message.send_message\",\n",
    "                  \"wd_message.receive_message\"])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}